package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.entity.Documento;
import edu.upc.clase.demo.service.DocumentoService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Carlos
 */
@Controller("documentoController")
public class DocumentoController {

    @Autowired
    private DocumentoService documentoService;
    private static Logger log = LoggerFactory.getLogger(DocumentoController.class);

    @RequestMapping("/documentos/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("documentos/index");
        List<Documento> documentos = documentoService.buscarTodos();
        CriterioBusqueda criterioBusqueda = new CriterioBusqueda();
        mav.addObject("documentos", documentos);
        mav.addObject("criterioBusqueda",criterioBusqueda);
        return mav;
    }
    
    @RequestMapping(value = "/documentos/new", method = RequestMethod.GET)
    public ModelAndView newDocumento() {
        ModelAndView mav = new ModelAndView("documentos/new");
        Documento documento = new Documento();
        mav.getModelMap().put("documento", documento);
        return mav;
    }

    @RequestMapping(value = "/documentos/new", method = RequestMethod.POST)
    public String createDocumento(@ModelAttribute("documento")Documento documento, SessionStatus status) {    
        documentoService.insertar(documento);
        status.setComplete();
        return "redirect:/pages/documentos/index";
    }
    
    @RequestMapping(value = "/documentos/edit", method = RequestMethod.GET)
    public ModelAndView editDocumento(@RequestParam("id")Integer id) {    
        ModelAndView mav = new ModelAndView("documentos/edit");
        Documento documento = documentoService.buscar(id);
        mav.getModelMap().put("documento", documento);
        return mav;        
    }
    
    @RequestMapping(value="/documentos/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("documento") Documento documento, SessionStatus status) {
        documentoService.actualizar(documento);
        status.setComplete();
        return "redirect:/pages/documentos/index";
    }
    
    @RequestMapping("/documentos/delete")
    public ModelAndView delete(@RequestParam("id")Integer id)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/documentos/index");
        Documento documento = documentoService.buscar(id);
        documentoService.eliminar(documento);
        return mav;
    }
    
    @RequestMapping(value = "/documentos/buscar", method=RequestMethod.POST)
    public ModelAndView buscar(@ModelAttribute("criterioBusqueda") CriterioBusqueda criterio, SessionStatus status) {
        ModelAndView mav = new ModelAndView("documentos/resultadoBusqueda");
        log.info("resultado = " + documentoService.buscarPorNombre(criterio.getNombre()).size());
        mav.getModel().put("resultado",documentoService.buscarPorNombre(criterio.getNombre()));
        return mav;
    }
    
}
